package nill

import (
	"bufio"
	"fmt"
	"io"
	"reflect"
	"testing"
)

func TestNil1(t *testing.T) {
	bufWriter := func() io.Writer {
		var w *bufio.Writer // w 有了类型
		fmt.Printf("w is nil => %t\n", w == nil)
		return w
	}()
	fmt.Printf("bufWriter is nil => %t\n", bufWriter == nil)
}

func TestNil2(t *testing.T) {
	bufWriter := func() io.Writer { // 或是这里不返回具体类型，返回interface{}
		var w *bufio.Writer
		if w == nil {
			// 直接返回nil 无类型
			return nil
		}
		return w
	}()
	if bufWriter != nil {
		bufWriter.Write([]byte("golang"))
	} else {
		fmt.Println("bufWriter is nil")
	}
}

func TestNil3(t *testing.T) {
	bufWriter := func() io.Writer {
		var w *bufio.Writer
		fmt.Printf("w is nil => %t\n", w == nil)
		return w
	}()
	fmt.Printf("bufWriter is nil => %t\n", bufWriter == nil)
	fmt.Printf("IsNil => bufWriter is nil => %t\n", reflect.ValueOf(bufWriter).IsNil())
}
